package org.dallasrowing.logbook.ui.util;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.fieldassist.IContentProposal;
import org.eclipse.jface.fieldassist.IContentProposalProvider;

public class MyContentProposalProvider implements IContentProposalProvider {
	private String[] proposals;

	public MyContentProposalProvider(String[] data) {
		this.proposals = data;
	}

	public IContentProposal[] getProposals(String content, int position) {
		List<ComboProposal> cpList = new ArrayList<ComboProposal>();

		for ( int i=0; i < proposals.length; i++ )
		{
			if ( isContentProposal( proposals[i], content) ){
				cpList.add( new ComboProposal( i, proposals[i]) );
			}
		}

		IContentProposal[] cpArray = new IContentProposal[cpList.size()];

		return cpList.toArray(cpArray);
	}

	private boolean isContentProposal(String candidate, String content) {
		if ( isEmpty(candidate) || isEmpty(content) ) {
			return false;
		} else {
			return candidate.toLowerCase().contains( content.toLowerCase() );
		}
	}
	
	private boolean isEmpty(String candidate) {
		return candidate == null || candidate.equals("");
	}

	public static final char[] getAlphaNumericChars() {
		String alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		String deleteChar = new String(new char[] {8});

		String combo = alphanum + deleteChar;

		return combo.toCharArray();
	}
	
	public class ComboProposal implements IContentProposal {

		private int index;
		private String content;

		public ComboProposal(int index, String content)
		{
			this.index = index;
			this.content = content;
		}

		public int getIndex()
		{
			return index;
		}

		public String getContent() {
			return content;
		}

		public int getCursorPosition() {
			// TODO Auto-generated method stub
			return 0;
		}

		public String getDescription() {
			return null;
		}

		public String getLabel() {
			return content;
		}
	}
	
}

